package example.demo.netty.channelHandler;

import com.alibaba.fastjson.JSON;
import example.demo.netty.JsonMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class FastJsonEncoder extends MessageToByteEncoder<JsonMessage> {

    @Override
    protected void encode(ChannelHandlerContext ctx, JsonMessage msg, ByteBuf out) throws Exception {
        String jsonMsgStr = JSON.toJSONString(msg);
        byte[] jsonMsgBytes = jsonMsgStr.getBytes();
        out.writeBytes(jsonMsgBytes);
        ctx.flush();
    }
}
